﻿#include "FITKCCXAdaptorModel.h"
#include "FITKCalculixINPWriter.h"
#include "FITKCCXAdaptorSet.h"
#include "FITKCCXAdaptorSurface.h"
#include "FITKCCXAdaptorSection.h"
#include "FITKCCXAdaptorInertia.h"
#include "FITKCCXAdaptorTransform.h"

#include "FITK_Kernel/FITKCore/FITKVec3DAlg.h"
#include "FITK_Kernel/FITKCore/FITKVec3D.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Kernel/FITKCore/FITKAbstractDataManager.hpp"

#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"

#include "FITK_Interface/FITKInterfaceModel/FITKAssInstance.h"
#include "FITK_Interface/FITKInterfaceModel/FITKElementLine.h"
#include "FITK_Interface/FITKInterfaceModel/FITKUnstructuredMesh.h"
#include "FITK_Interface/FITKInterfaceModel/FITKModelSet.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractElement.h"
#include "FITK_Interface/FITKInterfaceModel/FITKElementHex.h"
#include "FITK_Interface/FITKInterfaceModel/FITKElementQuad.h"
#include "FITK_Interface/FITKInterfaceModel/FITKElementTet.h"
#include "FITK_Interface/FITKInterfaceModel/FITKElementTri.h"
#include "FITK_Interface/FITKInterfaceModel/FITKMeshSurface.h"
#include "FITK_Interface/FITKInterfaceModel/FITKMeshSurfaceNode.h"
#include "FITK_Interface/FITKInterfaceModel/FITKMeshSurfaceElement.h"

#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAssembly.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMeshPartVTK.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionBeam.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionShellHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionSolidHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionAssignShellHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionAssignSolidHomogenous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractSection.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfile.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfileI.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfileManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInertiaPointMassInertia.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaEngineeringFeature.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInertiaManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaTransform.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialOrientation.h"

#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKSectionAssign.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"

namespace IO
{
    QString FITKCCXAdaptorModel::getAdaptorClass()
    {
        return "IO::FITKCCXAdaptorModel";
    }

    void FITKCCXAdaptorModel::setCaseData(AbaqusData::FITKDataCase * caseData)
    {
        _caseData = caseData;
    }

    void FITKCCXAdaptorModel::setModelInfoManager(FITKCCXModelInforManager * modelInfoManager)
    {
        _modelInfoManager = modelInfoManager;
    }

    bool FITKCCXAdaptorModel::adaptR()
    {
        AbaqusData::FITKAbaqusPart* part = dynamic_cast<AbaqusData::FITKAbaqusPart*>(_dataObj);
        if (part == nullptr || _reader == nullptr) return false;
        


        return false;
    }

    bool FITKCCXAdaptorModel::adaptW()
    {
        // 将 _dataObj 强制转换为 FITKAbaAssembly 类型
        _assemblyData = dynamic_cast<Interface::FITKAbaAssembly*>(_dataObj);

        // 检查是否缺少任何必需的数据对象
        if (!_assemblyData || !_writer || !_caseData || !_modelInfoManager)
        {
            _writer->consoleMessage(3, "Model Error: Required data objects are missing");
            return false;
        }

        // 有一个失败则返回 false
        bool isW = true;
        try {

            // 写入节点并检查操作是否成功
            isW &= this->writeNode();
            if (!isW)  _writer->consoleMessage(3, "Error: writeNode() failed");

            // 写入元素并检查操作是否成功
            isW &= this->writeElement();
            if (!isW)  _writer->consoleMessage(3, "Error: writeElement() failed");

            // 写入集合表面并检查操作是否成功
            isW &= this->writeSetSurf();
            if (!isW) _writer->consoleMessage(3, "Error: writeSetSurf() failed");

            // 写入截面并检查操作是否成功
            isW &= this->writeSection();
            if (!isW) _writer->consoleMessage(3, "Error: writeSection() failed");

            // 写入局部坐标系并检查操作是否成功
            isW &= this->writeTransformOrientation();
            if (!isW) _writer->consoleMessage(3, "Error: writeTransformOrientation() failed");

            // 写入惯量并检查操作是否成功
            isW &= this->writeInertia();
            if (!isW) _writer->consoleMessage(3, "Error: writeInertia() failed");
        }
        catch (const std::exception& e) {

            // 在控制台记录异常消息
            _writer->consoleMessage(3, QString("Exception caught during writing: %1").arg(e.what()));
            return false;
        }
        catch (...) {

            // 在控制台记录未知异常消息
            _writer->consoleMessage(3, "Unknown exception caught during writing");
            return false;
        }
        return isW;
    }

    bool FITKCCXAdaptorModel::writeNode()
    {
        // 获取数据流
        QTextStream* stream = _writer->getStream();

        // 检查 stream 是否有效
        if (!stream) {
            _writer->consoleMessage(3, "Error: Unable to get data stream");
            return false;
        }

        // 写入节点部分的头部信息
        *stream << "*Node" << endl;
        bool isW = true;

        // 写入实例节点，并检查操作是否成功
        isW &= writeInstanceNode();
        if (!isW) {
            _writer->consoleMessage(3, "Error: writeInstanceNode() failed");
        }

        // 尝试写入装配节点，并检查操作是否成功
        isW &= writeAssemblyNode();
        if (!isW) {
            _writer->consoleMessage(3, "Error: writeAssemblyNode() failed");
        }
        return isW;
    }

    bool FITKCCXAdaptorModel::writeInstanceNode()
    {
        // 获取实例数量
        int instanceNum = _assemblyData->getDataCount();
        if (instanceNum < 1) return true;
        bool ok = true;
        for (size_t i = 0; i < instanceNum; i++)
        {
            // 获取当前索引的实例数据
            auto instance = _assemblyData->getDataByIndex(i);
            if (instance == nullptr) continue;

            // 获取实例的模型
            auto model = instance->getModel();
            if (model == nullptr) continue;

            // 将模型强制转换为 FITKAbaqusPart 类型
            auto partData = dynamic_cast<AbaqusData::FITKAbaqusPart *>(model);
            if (partData == nullptr) continue;

            // 获取数据流
            QTextStream* stream = _writer->getStream();
            auto tempInstance = dynamic_cast<Interface::FITKAssemblyInstance*>(instance);
            if (tempInstance == nullptr) return false;

            // 初始化平移向量、旋转轴和旋转角度
            double  transform[3]{ 0,0,0 }, ax[3]{ 0,0,0 }, ay[3]{ 0,0,0 };
            double angle = 0;

            // 获取平移向量、旋转轴、旋转角度
            tempInstance->getTransfom(transform);
            tempInstance->getRotateAxis(ax, ay, &angle);
            //angle = tempInstance->getAngle();

            // 判断是否需要旋转，旋转是否为0，为0则不写
            bool isR = false;
            isR |= JudgeZero(ax, sizeof(ax) / sizeof(double));
            isR |= JudgeZero(ay, sizeof(ay) / sizeof(double));
            isR |= angle > 0;

            for (int j = 0; j < partData->getMesh()->getNodeCount(); ++j)
            {
                // 节点编号递增
                _nodeNumber++;

                // 获取当前节点
                auto partNode = partData->getMesh()->getNodeAt(j);
                if (partNode == nullptr) {
                    _writer->consoleMessage(3, "Warning: Part node is null");
                    continue; // 如果节点为空，则跳过
                }

                // 获取节点 ID
                int partNodeId = partNode->getNodeID();
                if (isR)
                {
                    // 计算旋转轴向量(终点减起点)
                    Core::FITKVec3D axis(ay[0] - ax[0], ay[1] - ax[1], ay[2] - ax[2]);

                    // 旋转节点
                    Core::FITKPoint rotatedNode = Core::RotateAroundAxis(*partNode, axis, angle*FITK_PI / 180.0);

                    // 平移坐标
                    Core::FITKPoint partNodeCoorTransfo(rotatedNode.x() + transform[0], rotatedNode.y() + transform[1], rotatedNode.z() + transform[2]);
                    *stream << _writer->int2String(_nodeNumber, 8) << ",";
                    *stream << _writer->double2String(partNodeCoorTransfo.x(), 9, 18) << ",";
                    *stream << _writer->double2String(partNodeCoorTransfo.y(), 9, 18) << ",";
                    *stream << _writer->double2String(partNodeCoorTransfo.z(), 9, 18) << endl;
                }
                else
                {
                    // 平移坐标
                    Core::FITKPoint partNodeCoorTransfo(partNode->x() + transform[0], partNode->y() + transform[1], partNode->z() + transform[2]);
                    *stream << _writer->int2String(_nodeNumber, 8) << ",";
                    *stream << _writer->double2String(partNodeCoorTransfo.x(), 9, 18) << ",";
                    *stream << _writer->double2String(partNodeCoorTransfo.y(), 9, 18) << ",";
                    *stream << _writer->double2String(partNodeCoorTransfo.z(), 9, 18) << endl;
                }

                // 添加节点映射信息
                QPair<int, int> instanceNodeMappingKey(instance->getDataObjectID(), partNodeId);
                _modelInfoManager->addNodeMapping(instanceNodeMappingKey, _nodeNumber);
            }
        }
        return ok;
    }

    bool FITKCCXAdaptorModel::writeAssemblyNode()
    {
        // 获取数据流
        QTextStream* stream = _writer->getStream();
        if (stream == nullptr) {
            _writer->consoleMessage(3, "Error: WriteAssemblyNode unable to get data stream");
            return false;
        }

        // 获取装配节点的数量
        int nodeNum = _assemblyData->getPoints()->getNodeCount();

        // 获取assemby对象的ID
        int assemblyID = _assemblyData->getDataObjectID();
        for (int i = 0; i < nodeNum; ++i)
        {
            // 获取当前节点
            Core::FITKNode *assemblyNode = _assemblyData->getPoints()->getNodeAt(i);
            if (assemblyNode == nullptr) {
                _writer->consoleMessage(3, QString("Warning: Assembly node at index %1 is null").arg(i));
                continue; // 如果节点为空，跳过
            }

            // 增加节点编号
            _nodeNumber++;

            // 获取节点ID
            int assemblyNodeID = assemblyNode->getNodeID();

            // 添加节点映射信息
            QPair<int, int> abaqusMappingKey(assemblyID, assemblyNodeID);
            _modelInfoManager->addNodeMapping(abaqusMappingKey, _nodeNumber);

            // 将节点信息写入流
            *stream << _writer->int2String(_nodeNumber, 8) << ",";
            *stream << _writer->double2String(assemblyNode->x(), 9, 18) << ",";
            *stream << _writer->double2String(assemblyNode->y(), 9, 18) << ",";
            *stream << _writer->double2String(assemblyNode->z(), 9, 18) << endl;
        }
        return true;
    }

    bool FITKCCXAdaptorModel::JudgeZero(double * d, int size)
    {
        for (int i = 0; i < size; ++i)
        {
            if (fabs(d[i]) > 0)
                return true;
        }
        return false;
    }

    bool FITKCCXAdaptorModel::writeElement()
    {
        // 获取数据流
        QTextStream* stream = _writer->getStream();
        if (stream == nullptr) {
            _writer->consoleMessage(3, "Error: WriteElement unable to get data stream");
            return false; // 如果无法获取数据流，返回 false
        }

        // 获取实例数量
        int instanceNum = _assemblyData->getDataCount();
        if (instanceNum < 1) {
            _writer->consoleMessage(3, "Warning: writeElement No instances found");
            return false; // 如果没有实例，返回 false
        }

        // 初始化写入的单元类型
        Interface::FITKAbaMeshPartVTK::AbaEleType writeT = Interface::FITKAbaMeshPartVTK::AbaEleType::AbaEleNone;
        for (size_t i = 0; i < instanceNum; i++)
        {
            // 获取当前索引的实例数据
            auto instance = _assemblyData->getDataByIndex(i);
            if (instance == nullptr) continue;

            // 获取实例的模型
            auto model = instance->getModel();
            if (model == nullptr) continue;

            // 将模型强制转换为 FITKAbaqusPart 类型
            auto partData = dynamic_cast<AbaqusData::FITKAbaqusPart *>(model);
            if (partData == nullptr) continue;

            // 获取网格数据
            auto mesh = partData->getMesh();
            if (mesh == nullptr) continue;

            // 获取单元数量
            int eleCount = mesh->getElementCount();
            if (eleCount < 1) continue;

            // 获取最大id的长度+1 作为字符宽度
            int elementMaxIDwidth = QString::number(mesh->getElementMaxID()).size() + 1;

            // 确定解析的单元类型
            Core::FITKEnumTransfer<Interface::FITKAbaMeshPartVTK::AbaEleType> abaEleTypeTrafer;

            bool isValid = false;
            for (int j = 0; j < eleCount; ++j)
            {
                // 获取单元类型
                auto type = mesh->getAbaElementType(j);
                if (type != writeT)
                {
                    // 如果单元类型发生变化，写入新的单元类型
                    auto eleTypeStr = abaEleTypeTrafer.toStrting(type, isValid);
                    if (!isValid) continue;
                    *stream << "*Element, type=" << eleTypeStr << endl;
                    writeT = type;
                }

                // 获取当前单元
                auto ele = mesh->getElementAt(j);
                if (ele == nullptr) {
                    _writer->consoleMessage(3, QString("Warning: Element at index %1 is null").arg(j));
                    continue; // 如果单元为空，跳过
                }

                // 增加单元编号
                _elementNumber++;

                // 添加单元映射信息
                QPair<int, int> elementMappingKey(instance->getDataObjectID(), ele->getEleID());
                _modelInfoManager->addElementMapping(elementMappingKey, _elementNumber);

                // 将单元编号写入流
                *stream << _writer->int2String(_elementNumber, elementMaxIDwidth);

                // 获取单元包含的节点数量
                int nodeCount = ele->getNodeCount();
                for (int k = 0; k < nodeCount; ++k)
                {
                    QPair<int, int> nodeMappingKey(instance->getDataObjectID(), ele->getNodeID(k));
                    int nodeMappingValue = _modelInfoManager->getNodeMappingValue(nodeMappingKey);

                    // 将节点编号写入流
                    *stream << "," << _writer->int2String(nodeMappingValue, elementMaxIDwidth);
                }
                *stream << endl;
            }
        }
        return true;
    }

    bool FITKCCXAdaptorModel::writeSetSurf()
    {
        bool isW = true;

        try {
            // 尝试写入实例集表面数据
            if (!writeInstanceSetSurf()) {
                _writer->consoleMessage(3, "Error: writeInstanceSetSurf() failed");
                isW = false;
            }

            // 尝试写入装配集表面数据
            if (!writeAssemblySetSurf()) {
                _writer->consoleMessage(3, "Error: writeAssemblySetSurf() failed");
                isW = false;
            }
        }
        catch (const std::exception& e) {
            // 捕获标准异常并记录错误信息，将返回值设为 false
            _writer->consoleMessage(3, QString("Exception caught during writeSetSurf: %1").arg(e.what()));
            isW = false;
        }
        catch (...) {
            // 捕获所有其他类型的异常并记录错误信息，将返回值设为 false
            _writer->consoleMessage(3, "Unknown exception caught during writeSetSurf");
            isW = false;
        }

        return isW;
    }

    bool FITKCCXAdaptorModel::writeInstanceSetSurf()
    {
        // 获取装配数据中的实例数量
        int instanceNum = _assemblyData->getDataCount();
        if (instanceNum < 1) return true;
        bool ok = true;
        for (size_t i = 0; i < instanceNum; i++)
        {
            // 获取当前实例
            auto instance = _assemblyData->getDataByIndex(i);
            if (instance == nullptr) continue;

            // 获取当前实例的模型
            auto model = instance->getModel();
            if (model == nullptr) continue;

            // 将模型转换为FITKAbaqusPart类型
            auto partData = dynamic_cast<AbaqusData::FITKAbaqusPart *>(model);
            if (partData == nullptr) continue;

            // 获取部件数据的组件管理器
            auto setSurfManager = partData->getComponentManager();
            if (!setSurfManager)return false;

            // 获取组件管理器中的组件数量
            int count = setSurfManager->getDataCount();
            for (int j = 0; j < count; ++j)
            {
                // 获取当前组件
                auto data = setSurfManager->getDataByIndex(j);

                // 如果是模型集合，调用写入集合函数
                Interface::FITKModelSet* set = dynamic_cast<Interface::FITKModelSet*>(data);
                if (set)
                    ok = writeSet(set, instance->getDataObjectID());

                // 如果是网格表面，调用写入表面函数
                Interface::FITKMeshSurface* surface = dynamic_cast<Interface::FITKMeshSurface*>(data);
                if (surface)
                    ok = writeSurface(surface, instance->getDataObjectID());
            }
        }
        return ok;
    }

    bool FITKCCXAdaptorModel::writeAssemblySetSurf()
    {
        auto setSurfManager = _assemblyData->getComponentManager();
        if (!setSurfManager)return true;
        int count = setSurfManager->getDataCount();
        bool ok = true;
        for (int i = 0; i < count; ++i)
        {
            auto data = setSurfManager->getDataByIndex(i);
            Interface::FITKModelSet* set = dynamic_cast<Interface::FITKModelSet*>(data);
            if (set)
                ok = writeSet(set, _assemblyData->getDataObjectID());
            Interface::FITKMeshSurface* surface = dynamic_cast<Interface::FITKMeshSurface*>(data);
            if (surface)
                ok = writeSurface(surface, _assemblyData->getDataObjectID());
        }
        return ok;
    }

    bool FITKCCXAdaptorModel::writeSet(Interface::FITKModelSet * set, const int &objectDataID)
    {
        bool ok = false;
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKCCXAdaptorSet>("CCXINP", set);
        if (adaptor == nullptr) return false;
        adaptor->setFileWriter(_writer);
        adaptor->setDataObject(set);
        adaptor->setMappingKeyObjectDataID(objectDataID);
        adaptor->setModelInfoManager(_modelInfoManager);
        adaptor->setAssemblyManager(_assemblyData);
        adaptor->setSetNumber(_setNumber);
        ok = adaptor->adaptW();
        _setNumber = adaptor->getSetNumber();
        delete adaptor;
        return ok;
    }

    bool FITKCCXAdaptorModel::writeSurface(Interface::FITKMeshSurface * surface, const int &mappingKeyDataObjectID)
    {
        auto setSurfManager = _assemblyData->getComponentManager();
        if (!setSurfManager)return false;
        bool ok = false;
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKCCXAdaptorSurface>("CCXINP", surface);
        if (adaptor == nullptr) return false;
        adaptor->setFileWriter(_writer);
        adaptor->setDataObject(surface);
        adaptor->setAssemblyPartManager(_assemblyData, _caseData->getPartManager());
        adaptor->setModelInfoManager(_modelInfoManager);
        adaptor->setSurfaceNumber(_surfaceNumber);
        adaptor->setMappingKeyDataObjectID(mappingKeyDataObjectID);
        ok = adaptor->adaptW();
        _surfaceNumber = adaptor->getSurfaceNumber();
        delete adaptor;
        if (!ok)
            _writer->consoleMessage(3, QString("write Surface Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
        return ok;
    }

    bool FITKCCXAdaptorModel::writeSection()
    {
        int instanceNum = _assemblyData->getDataCount();
        if (instanceNum < 1) return true;
        bool ok = true;
        for (size_t i = 0; i < instanceNum; i++)
        {
            auto instance = _assemblyData->getDataByIndex(i);
            if (instance == nullptr) continue;
            int instanceID = instance->getDataObjectID();

            auto model = instance->getModel();
            if (model == nullptr) continue;

            auto partData = dynamic_cast<AbaqusData::FITKAbaqusPart *>(model);
            if (partData == nullptr) continue;

            // 调用section适配器读写
            auto sectionManager = _caseData->getSectionManager();
            if (!sectionManager) return false;
            auto sectionAssignManager = partData->getSectionAssignManager();
            if (!sectionAssignManager) return false;

            // 通过指派器来获取section并写出
            int setDataCount = sectionAssignManager->getDataCount();
            for (int j = 0; j < setDataCount; ++j)
            {
                bool ok = false;
                auto sectionAssign = sectionAssignManager->getDataByIndex(j);
                if (!sectionAssign->isValid())continue;
                int sectionID = sectionAssign->getSectionID();
                auto s = sectionManager->getDataByID(sectionID);

                Interface::FITKAbaAbastractSection* section = dynamic_cast<Interface::FITKAbaAbastractSection*>(s);
                if (section == nullptr)continue;

                auto adaptor = FITKIOADAPTORFACTORY->createT<FITKCCXAdaptorSection>("CCXINP", section);
                if (adaptor == nullptr) return false;

                adaptor->setFileWriter(_writer);
                adaptor->setDataObject(section);
                adaptor->setSetManager(partData->getComponentManager());
                adaptor->setModelInfoManager(_modelInfoManager);
                adaptor->setSetManager(partData->getComponentManager());
                adaptor->setSectionAssignAndProfile(sectionAssign, nullptr);
                adaptor->setMappingKeyObjectDataID(instanceID);
                adaptor->setPart(partData);
                ok = adaptor->adaptW();
                delete adaptor;
                if (!ok) _writer->consoleMessage(3, QString("Write Section Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
                if (!ok)return false;
            }
        }
        return ok;
    }

    bool FITKCCXAdaptorModel::writeInertia()
    {
        bool isW = true;

        try {
            // 尝试写入实例集表面数据
            if (!writeInstanceInertia()) {
                _writer->consoleMessage(3, "Error: writeInstanceInertia() failed");
                isW = false;
            }

            // 尝试写入装配集表面数据
            if (!writeAssemblyInertia()) {
                _writer->consoleMessage(3, "Error: writeAssemblyInertia() failed");
                isW = false;
            }
        }
        catch (const std::exception& e) {
            // 捕获标准异常并记录错误信息，将返回值设为 false
            _writer->consoleMessage(3, QString("Exception caught during writeSetSurf: %1").arg(e.what()));
            isW = false;
        }
        catch (...) {
            // 捕获所有其他类型的异常并记录错误信息，将返回值设为 false
            _writer->consoleMessage(3, "Unknown exception caught during writeSetSurf");
            isW = false;
        }

        return isW;
    }

    bool FITKCCXAdaptorModel::writeInstanceInertia()
    {
        // 获取装配数据中的实例数量
        int instanceNum = _assemblyData->getDataCount();
        if (instanceNum < 1) return true;
        bool ok = true;
        for (size_t i = 0; i < instanceNum; i++)
        {
            // 获取当前实例
            auto instance = _assemblyData->getDataByIndex(i);
            if (instance == nullptr) continue;

            // 获取当前实例的模型
            auto model = instance->getModel();
            if (model == nullptr) continue;

            // 将模型转换为FITKAbaqusPart类型
            auto partData = dynamic_cast<AbaqusData::FITKAbaqusPart *>(model);
            if (partData == nullptr) continue;

            //获取装配下的工程特性
            Interface::FITKAbaPartEngineeringFeature* engineeringFeatureAssembly = partData->getPartEngineerFeature();

            //获取惯量管理器
            Interface::FITKAbaInertiaManager* inertiaManager = engineeringFeatureAssembly->getInertiaManager();
            int count = inertiaManager->getDataCount();
            for (int i = 0; i < count; ++i)
            {
                Interface::FITKAbaAbstractInertia* inertia = inertiaManager->getDataByIndex(i);
                if (!inertia)continue;
                auto adaptor = FITKIOADAPTORFACTORY->createT<FITKCCXAdaptorInertia>("CCXINP", inertia);
                if (adaptor == nullptr) return false;
                adaptor->setFileWriter(_writer);
                adaptor->setDataObject(inertia);
                adaptor->setWriteElementId(&_elementNumber);
                adaptor->setModelInfoManager(_modelInfoManager);
                adaptor->setPartData(partData);
                adaptor->setMappingKeyObjectDataID(instance->getDataObjectID());
                bool ok = adaptor->adaptW();
                delete adaptor;
                if (!ok)
                    _writer->consoleMessage(3, QString("write Inertia Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
                if (!ok) return false;
            }
        }

        return true;
    }

    bool FITKCCXAdaptorModel::writeAssemblyInertia()
    {
        if (!_assemblyData)return false;
        //获取装配下的工程特性
        Interface::FITKAbaAssemblyEngineeringFeature* engineeringFeatureAssembly = _assemblyData->getAssemblyEngineerFeature();
        //获取惯量管理器
        Interface::FITKAbaInertiaManager* inertiaManager = engineeringFeatureAssembly->getInertiaManager();
        int count = inertiaManager->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbaAbstractInertia* inertia = inertiaManager->getDataByIndex(i);
            if (!inertia)continue;
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKCCXAdaptorInertia>("CCXINP", inertia);
            if (adaptor == nullptr) return false;
            adaptor->setFileWriter(_writer);
            adaptor->setDataObject(inertia);
            adaptor->setWriteElementId(&_elementNumber);
            adaptor->setAssemblyData(_assemblyData);
            adaptor->setMappingKeyObjectDataID(_assemblyData->getDataObjectID());
            adaptor->setModelInfoManager(_modelInfoManager);
            bool ok = adaptor->adaptW();
            delete adaptor;
            if (!ok)
                _writer->consoleMessage(3, QString("write Inertia Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            if (!ok) return false;
        }
        return true;
    }

    bool FITKCCXAdaptorModel::writeTransformOrientation()
    {
        bool isW = true;

        try {
            // 写入实例集局部坐标系
            if (!writeInstanceOrientation()) {
                _writer->consoleMessage(3, "Error: writeInstanceTransformOrientation() failed");
                isW = false;
            }

            // 尝试写入装配集局部坐标系
            if (!writeAssemblyTransform()) {
                _writer->consoleMessage(3, "Error: writeAssemblyTransformOrientation() failed");
                isW = false;
            }
        }
        catch (const std::exception& e) {
            // 捕获标准异常并记录错误信息，将返回值设为 false
            _writer->consoleMessage(3, QString("Exception caught during writeSetSurf: %1").arg(e.what()));
            isW = false;
        }
        catch (...) {
            // 捕获所有其他类型的异常并记录错误信息，将返回值设为 false
            _writer->consoleMessage(3, "Unknown exception caught during writeSetSurf");
            isW = false;
        }

        return isW;
    }

    bool FITKCCXAdaptorModel::writeInstanceOrientation()
    {
        // 获取装配数据中的实例数量
        int instanceNum = _assemblyData->getDataCount();
        if (instanceNum < 1) return true;
        bool ok = true;
        for (size_t i = 0; i < instanceNum; i++)
        {
            // 获取当前实例
            auto instance = _assemblyData->getDataByIndex(i);
            if (instance == nullptr) continue;

            // 获取当前实例的模型
            auto model = instance->getModel();
            if (model == nullptr) continue;

            // 将模型转换为FITKAbaqusPart类型
            auto partData = dynamic_cast<AbaqusData::FITKAbaqusPart *>(model);
            if (partData == nullptr) continue;

            //写出材料指派方向
            Interface::FITKAbaMaterialOrientationManager* materialOrientationManager = partData->getMaterialOrientationManager();
            int count = materialOrientationManager->getDataCount();
            for (int i = 0; i < count; ++i)
            {
                Interface::FITKAbaMaterialOrientation* transform = materialOrientationManager->getDataByIndex(i);
                if (!transform)continue;
                auto adaptor = FITKIOADAPTORFACTORY->createT<FITKCCXAdaptorTransform>("CCXINP", transform);
                if (adaptor == nullptr) return false;

                //设置数据
                adaptor->setFileWriter(_writer);
                adaptor->setDataObject(transform);
                adaptor->setPartData(partData, true);

                //开始写出
                bool ok = adaptor->adaptW();
                delete adaptor;
                if (!ok)
                {
                    _writer->consoleMessage(3, QString("write Orientation Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
                    return false;
                }

            }
            //写出局部坐标系
            auto transformManager = partData->getCoordinateSystemManager();
            count = transformManager->getDataCount();
            if (transformManager->getDataCount() != 0)
            {
                _writer->consoleMessage(2, "Calculix does not support transform when writing part transform");
            }
            //for (int i = 0; i < count; ++i)
            //{
            //    Interface::FITKAbaTransform* transform = dynamic_cast<Interface::FITKAbaTransform*>(transformManager->getDataByIndex(i));
            //    if (!transform)continue;
            //    auto adaptor = FITKIOADAPTORFACTORY->createT<FITKCCXAdaptorTransform>("CCXINP", transform);
            //    if (adaptor == nullptr) return false;
            //    //设置数据
            //    adaptor->setFileWriter(_writer);
            //    adaptor->setDataObject(transform);
            //    adaptor->setPartData(partData);
            //    //开始写出
            //    bool ok = adaptor->adaptW();
            //    delete adaptor;
            //    if (!ok)
            //    {
            //        _writer->consoleMessage(3, QString("write Transform Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            //        return false;
            //    }
            //}
        }
        return true;
    }

    bool FITKCCXAdaptorModel::writeAssemblyTransform()
    {
        if (!_caseData)return false;
        Interface::FITKAbaTransformManager* transformManager = _assemblyData->getTransformManager();
        int count = transformManager->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbaTransform* transform = transformManager->getDataByIndex(i);
            if (!transform)continue;
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKCCXAdaptorTransform>("CCXINP", transform);
            if (adaptor == nullptr) return false;
            adaptor->setFileWriter(_writer);
            adaptor->setDataObject(transform);
            adaptor->setCaseData(_caseData);
            adaptor->setModelInfoManager(_modelInfoManager);
            bool ok = adaptor->adaptW();
            delete adaptor;
            if (!ok)
                _writer->consoleMessage(3, QString("write Transform Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            if (!ok) return false;
        }
        return true;
    }
}